import  * as regular  from '@/utils/regular.js';

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
	return /^(https?:|mailto:|tel:)/.test(path)
}
// 手机号校验
/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validMobile(str) {
	return regular.PhoneRegular.test(str)
}



// validIdCard 身份证号验证

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validIdCard(str) {
	return regular.IDCardRegular.test(str)
}

export function checkIdCard(value, callback,myCallback) {
	if (typeof myCallback !="function"){
		myCallback = ()=>{}
	}
	let msg = ""
	let callData = {
		status: false,
		msg:"",
	}
	// 1 "验证通过!", 0 //校验不通过
	const format =
		/^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;

	//区位码校验
	//出生年月日校验   前正则限制起始年份为1900;
	const year = value.substr(6, 4), //身份证年
		month = value.substr(10, 2), //身份证月
		date = value.substr(12, 2), //身份证日
		time = Date.parse(month + '-' + date + '-' + year), //身份证日期时间戳date
		now_time = Date.parse(new Date()), //当前时间戳
		dates = (new Date(year, month, 0)).getDate(); //身份证当月天数

	//校验码判断
	const c = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //系数
	const b = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); //校验码对照表
	const id_array = value.split("");
	let sum = 0;
	for (let k = 0; k < 17; k++) {
		sum += parseInt(id_array[k]) * parseInt(c[k]);
	}
	//号码规则校验
	if (!format.test(value)) {
		msg = '身份证号输入有误！'
		callData.msg = msg
		myCallback(callData)
		return callback(new Error(msg))
	} else if (time > now_time || date > dates) {
		msg = '出生日期不合规！请输入正确的身份证号！'
		callData.msg = msg
		myCallback(callData)
		return callback(new Error(msg))
	} else if (id_array[17].toUpperCase() != b[sum % 11].toUpperCase()) {
		msg = '身份证校验不合规！请输入正确的身份证号！'
		callData.msg = msg
		myCallback(callData)
		return callback(new Error(msg))
	}
	callData.status = true
	callData.msg = true
	myCallback(callData)
	return callback();
}


// email
/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validEmail(str) {
	return regular.EmailRegular.test(str)
}

// password
/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validPassword(str) {
	return regular.PassWordRegular.test(str)
}

// 小数点后两位
/**
 * @param {string} val
 * @returns {Boolean}
 */
export function validNumber2(str) {
	return regular.Number2.test(str)
}


// 银行卡校验
/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validBankCard(str) {
	return regular.BankCardRegular.test(str)
}

// 社会统一信用代码
export function validCreditCode(value){
	return regular.SocialUniformCreditCode.test(value)
}